<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="单位名称" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务项目" prop="cooperationProjects">
        <el-select
          v-model="queryParams.cooperationProjects"
          placeholder="请选择服务项目"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.service_items"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="createTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['company:profitSharing:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="profitList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单位名称" align="center" prop="comName" min-width="250" />
      <el-table-column label="医院级别" align="center" prop="comLevel">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.company_level"
            :value="scope.row.comLevel"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="服务项目"
        align="center"
        prop="cooperationProjects"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.service_items"
            :value="scope.row.cooperationProjects"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="归属业务员"
        align="center"
        prop="attributableSalesman"
      />
      <el-table-column label="渠道抽成" align="center" prop="channelFee">
        <template slot-scope="scope">
          <span>{{ (scope.row.channelFee || 0) + "%" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位抽成" align="center" prop="comFee">
        <template slot-scope="scope">
          <span>{{ (scope.row.comFee || 0) + "%" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="专家剩余" align="center" prop="expertSurplus">
        <template slot-scope="scope">
          <span>{{ (scope.row.expertSurplus || 0) + "%" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['company:profitSharing:edit']"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单位分润对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="300px">
        <div class="pb-20">单位名称：{{ form.comName }}</div>
        <el-tabs v-model="activeName" type="border-card">
          <el-tab-pane label="单位专家分润设置" name="first">
            <p v-if="form.cooperationProjects === '1'">即时咨询配置：</p>
            <p v-else-if="form.cooperationProjects === '3'">服务项目配置：</p>
            <p v-else>预约咨询配置：</p>
            <el-form-item
              label="该渠道单笔订单利润，上级渠道抽成"
              prop="channelFee"
              required
            >
              <el-input-number
                size="mini"
                :precision="0"
                v-model="form.channelFee"
                disabled
                placeholder="请输入数字"
                :min="0"
                :max="100"
              />
              &nbsp;&nbsp;<span>%</span>
            </el-form-item>
            <el-form-item label="单位抽成" prop="comFee" required>
              <el-input-number
                size="mini"
                :precision="0"
                v-model="form.comFee"
                placeholder="请输入数字"
                :min="0"
                :max="100 - form.channelFee"
              />
              &nbsp;&nbsp;<span>%</span>
            </el-form-item>
            <el-form-item
              label="专家抽成"
              prop="expertSurplus"
              required
            >
              <el-input-number
                size="mini"
                :precision="0"
                v-model="form.expertSurplus"
                placeholder="请输入数字"
                :min="100 - form.channelFee - form.comFee"
                :max="100 - form.channelFee - form.comFee"
              />
              &nbsp;&nbsp;<span>%</span>
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProfit,
  getProfit,
  delProfit,
  addProfit,
  updateProfit,
} from "@/api/profit/comprofit";

export default {
  name: "ProfitSharing",
  dicts: ["service_items", "company_level"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      activeName: "first",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      createTime: "",
      // 总条数
      total: 0,
      // 单位分润表格数据
      profitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: null,
        createTimeStart: null,
        createTimeEnd: null,
        comId: null,
        cooperationProjects: null,
        attributableSalesman: null,
        channelFee: null,
        comFee: null,
        expertSurplus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  watch: {
    "form.channelFee"(nval, oval) {
      if (oval && (nval || nval === 0)) {
        this.form.comFee = 0;
        this.form.expertSurplus = 0;
      } else if (!nval && nval !== 0) {
        this.form.channelFee = 0;
      }
    },
    "form.comFee"(nval, oval) {
      if (!nval && nval !== 0) {
        this.form.comFee = 0;
      }
      this.form.expertSurplus = 100 - this.form.channelFee - this.form.comFee;
    },
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询单位分润列表 */
    getList() {
      this.queryParams.createTimeStart = this.createTime && this.createTime.length ? this.createTime[0] : "";
      this.queryParams.createTimeEnd = this.createTime && this.createTime.length ? this.createTime[1] : "";
      this.loading = true;
      listProfit(this.queryParams).then((response) => {
        this.profitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        comId: null,
        cooperationProjects: null,
        attributableSalesman: null,
        channelFee: null,
        comFee: null,
        expertSurplus: null,
        createTime: null,
        comName: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单位分润";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProfit(id).then((response) => {
        this.form = response.data;
        this.form.channelFee = this.form.channelFee || 0;
        this.form.comFee = this.form.comFee || 0;
        this.form.expertSurplus = this.form.expertSurplus || 0;
        this.form.comName = row.comName;
        this.open = true;
        this.title = "分润配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            let params = JSON.parse(JSON.stringify(this.form));
            params.channelFee = this.form.channelFee || 0;
            params.comFee = this.form.comFee || 0;
            params.expertSurplus = this.form.expertSurplus || 0;

            updateProfit(params).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProfit(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除单位分润编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProfit(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "company/profit/export",
        {
          ...this.queryParams,
        },
        `单位分润_${this.parseTime(new Date(),'{y}-{m}-{d}')}.xlsx`
      );
    },
  },
};
</script>
